package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.github.mikephil.charting.charts.BarChart;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyEvent1ListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.BarDataBean;
import patrol.mobile.com.mobilepatrol.bean.Event1ListBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.bean.YjsjtjBarBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.BarChartUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by K on 2018/9/14.
 */
public class EmergencyEvent1Activity extends CommonListActivity {
    private View itemTopView;
    private View itemCharView;
    private MySpinner spOrgName;
    private MySpinner spYear;
    private ImageView ivSearch;
    private BarChart barChart;
    private String orgId = "";
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private List<String> orgNameList = new ArrayList<>();
    private List<String> orgIdList = new ArrayList<>();
    private List<String> xData = new ArrayList<>();

    @Override
    public void initView() {
        super.initView();
        itemTopView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_event1_top, null);
        itemCharView = LayoutInflater.from(this).inflate(R.layout.head_event1, null);
        spOrgName = itemTopView.findViewById(R.id.spOrgName);
        spYear = itemTopView.findViewById(R.id.spYear);
        ivSearch = itemTopView.findViewById(R.id.ivSearch);
        barChart = itemCharView.findViewById(R.id.mBarChart);
        flContent.addView(itemTopView);
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencyEvent1ListAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_EVENT_1;
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
        getOrg();
        xData.add("较小");
        xData.add("较大");
        xData.add("重大");
        xData.add("一般");
        xData.add("特重大");
        mAdapter.addHeaderView(itemCharView);
    }

    @Override
    public void setListener() {
        super.setListener();
        spYear.setmDatas(TimeUtils.getYearList(), new MySpinner.CallBack() {
            @Override
            public void onSelect(int position) {
                spYear.getmText().setText(TimeUtils.getYearList().get(position));
                getData();
            }
        });
        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getData();
            }
        });
    }

    /**
     * 张志敏:先请求一次
     */
    private void getData() {
        String orgNme = spOrgName.getmText().getText().toString();
        String year = spYear.getmText().getText().toString();
        for (int i = 0; i < orgNameList.size(); i++) {
            if (orgNme.equals(orgNameList.get(i))) {
                orgId = orgIdList.get(i);
                requestDataChar(orgId, year);
                requestData(orgId, year);
                return;
            }
        }
    }

    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
                //orgNameList = new ArrayList<>();
                orgNameList.clear();
                orgIdList.clear();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    orgNameList.add(listBean.getOrgName());
                    orgIdList.add(listBean.getId());
                }
                if (orgNameList != null && orgNameList.size() > 0) {
                    spOrgName.getmText().setText(orgNameList.get(0));
                }

                getData();

                spOrgName.setmDatas(orgNameList, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        getData();
                    }
                });


            }
        });
    }

    private int[] getMPColors() {
        int[] colors = new int[7];
        colors[0] = getResources().getColor(R.color.bar_color01);
        colors[1] = getResources().getColor(R.color.bar_color02);
        colors[2] = getResources().getColor(R.color.bar_color03);
        colors[3] = getResources().getColor(R.color.bar_color04);
        colors[4] = getResources().getColor(R.color.bar_color05);
        colors[5] = getResources().getColor(R.color.bar_color06);
        colors[6] = getResources().getColor(R.color.bar_color07);
        return colors;
    }

    private void requestData(String orgId, String year) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_EVENT_LIST1);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("year", year);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                List<Event1ListBean> event1ListBeanList = GsonUtil.jsonToList(listArray.toString(), Event1ListBean.class);
                mAdapter.updateItems(event1ListBeanList);
            }
        });
    }


    private void requestDataChar(final String orgId, final String year) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_EVENT_1);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("year", year);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                YjsjtjBarBean yjsjtjBarBean = new Gson().fromJson(response, YjsjtjBarBean.class);
                List<BarDataBean> barDataBeans = new ArrayList<>();
                if (null != yjsjtjBarBean && yjsjtjBarBean.getList().size() > 0) {
                    BarDataBean barDataBean1 = new BarDataBean();
                    barDataBean1.setxName(xData.get(0));
                    barDataBean1.setfValue(yjsjtjBarBean.getList().get(0).getJx());
                    barDataBeans.add(barDataBean1);

                    BarDataBean barDataBean2 = new BarDataBean();
                    barDataBean2.setxName(xData.get(1));
                    barDataBean2.setfValue(yjsjtjBarBean.getList().get(0).getJd());
                    barDataBeans.add(barDataBean2);

                    BarDataBean barDataBean3 = new BarDataBean();
                    barDataBean3.setxName(xData.get(2));
                    barDataBean3.setfValue(yjsjtjBarBean.getList().get(0).getZd());
                    barDataBeans.add(barDataBean3);

                    BarDataBean barDataBean4 = new BarDataBean();
                    barDataBean4.setxName(xData.get(3));
                    barDataBean4.setfValue(yjsjtjBarBean.getList().get(0).getYb());
                    barDataBeans.add(barDataBean4);

                    BarDataBean barDataBean5 = new BarDataBean();
                    barDataBean5.setxName(xData.get(4));
                    barDataBean5.setfValue(yjsjtjBarBean.getList().get(0).getTzd());
                    barDataBeans.add(barDataBean5);
                }
                BarChartUtil.setBarChart(barChart, barDataBeans, getMPColors(), "数量");
            }
        });
    }

}
